﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Xml;
using BEGDC.Utility.NHibernate;
using Castle.Facilities.NHibernateIntegration;
using Castle.Services.Transaction;
using NHibernate;

namespace BEGDC.PreContract.BLL
{
    [Transactional]
    public partial class SalesFeedback : ISalesFeedback
    {
        private ISessionFactory factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public SalesFeedback(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }

        //Get by id
        [Transaction(TransactionMode.Requires)]
        public BEGDC.PreContract.DAL.SalesFeedback GetSalesFeedback(System.Int32 salesFeedbackID)
        {
            BEGDC.PreContract.DAL.SalesFeedback salesFeedback = BEGDC.PreContract.DAL.SalesFeedback.TryFind(salesFeedbackID);
            return salesFeedback;
        }

        //Get by filter
        [Transaction(TransactionMode.Requires)]
        public DataTable GetSalesFeedback_DataTable(string filter)
        {
            using (ISession session = factory.OpenSession())
            {
                string strSql = "SELECT * FROM SalesFeedback";
                if (filter.Trim() != string.Empty)
                    strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
        }

        //Delete
        [Transaction(TransactionMode.Requires)]
        public void DeleteSalesFeedback(System.Int32 salesFeedbackID)
        {
            try
            {
                BEGDC.PreContract.DAL.SalesFeedback salesFeedback = BEGDC.PreContract.DAL.SalesFeedback.TryFind(salesFeedbackID);
                salesFeedback.Delete();
            }
            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("SalesFeedback", string.Format("Error occured when deleting the SalesFeedback, error information: {0}.", ex.Message), ex);
            }
        }

        //Add
        [Transaction(TransactionMode.Requires)]
        public int AddSalesFeedback(
            BEGDC.PreContract.DAL.PreContractInfo precontractID,
            BEGDC.PreContract.DAL.DicItem signType,
            BEGDC.PreContract.DAL.DicItem treatmentWay,
            Nullables.NullableDateTime operationDate,
            BEGDC.PreContract.DAL.DicItem invoiceStatus,
            BEGDC.PreContract.DAL.DicItem cancelProcess,
            BEGDC.PreContract.DAL.DicItem problemLV1,
            BEGDC.PreContract.DAL.DicItem problemLV2,
            System.String comments,
            BEGDC.PreContract.DAL.DicItem dashboardIssueType,
            System.String peopleInCharge,
            BEGDC.PreContract.DAL.Employee creator)
        {
            try
            {
                BEGDC.PreContract.DAL.SalesFeedback salesFeedback = new BEGDC.PreContract.DAL.SalesFeedback();
                salesFeedback.PreContractID = precontractID;
                salesFeedback.SignType = signType;
                salesFeedback.TreatmentWay = treatmentWay;
                salesFeedback.OperationDate = operationDate;
                salesFeedback.InvoiceStatus = invoiceStatus;
                salesFeedback.CancelProcess = cancelProcess;
                salesFeedback.ProblemLV1 = problemLV1;
                salesFeedback.ProblemLV2 = problemLV2;
                salesFeedback.Comments = comments;
                salesFeedback.DashboardIssueType = dashboardIssueType;
                salesFeedback.PeopleInCharge = peopleInCharge;
                salesFeedback.Creator = creator;
                salesFeedback.CreateTime = DateTime.Now;
                salesFeedback.CreateAndFlush();

                return salesFeedback.ID;
            }
            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("SalesFeedback", string.Format("Error occured when adding a new SalesFeedback, error information: {0}.", ex.Message), ex);
            }
        }


        //Update
        [Transaction(TransactionMode.Requires)]
        public bool UpdateSalesFeedback()
        {
            try
            {
                //TOADD: add code here...
                return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("SalesFeedback", string.Format("Error occured when updating the SalesFeedback, error information: {0}.", ex.Message), ex);
            }
        }
    }
}
